package com.example.pre.business.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.pre.business.LoginBusiness;
import com.example.pre.exception.ServiceException;
import com.example.pre.model.User;
import com.example.pre.service.UserService;

@Service
@Transactional(rollbackFor = Exception.class)
public class LoginBusinessImpl implements LoginBusiness {

	@Autowired
	private UserService userService;

	@Override
	public boolean adminLogin(String account, String password) throws Exception {
		return "admin".equals(account) || "123456".equals(password);
	}

	@Override
	public User userLogin(String account, String password) throws Exception {
		Map<String, Object> selectMap = new HashMap<>();
		selectMap.put("account", account);
		Collection<User> users = userService.listByCondition(selectMap);
		if (CollectionUtils.isEmpty(users)) {
			return null;
		}
		User user = users.iterator().next();
		if (!password.equals(user.getPassword())) {
			throw new ServiceException("密码错误", "密码错误", -500);
		}
		return user;
	}

}
